/**
 * 
 */
package com.blog.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.blog.mapper.SysUserMapper;
import com.blog.pojo.SysUser;
import com.blog.pojo.SysUserExample;
import com.blog.service.SysUserRoleService;
import com.blog.service.SysUserService;

/**
 * @author zyj
 *
 */
@Service
public class SysUserServiceIml implements SysUserService {
	
	@Autowired 
	private SysUserMapper sysUserMapper;
    @Autowired 
    private SysUserRoleService sysUserRoleService;

	@Override
	public String getPassword(String name) {
		SysUser user = getByName(name);
		if(null == user) {
			return null;
		}
		return user.getPassword();
	}

	@Override
	public SysUser getByName(String name) {
		SysUserExample example = new SysUserExample();
		example.createCriteria().andNameEqualTo(name);
		List<SysUser> users = sysUserMapper.selectByExample(example);
		if(users.isEmpty()) {
			return null;
		}
		return users.get(0);
	}

	@Override
	public List<SysUser> list() {
		SysUserExample example = new SysUserExample();
		example.setOrderByClause("id desc");
		return sysUserMapper.selectByExample(example);
	}

	@Override
	public void add(SysUser user) {
		sysUserMapper.insert(user);

	}

	@Override
	public void delete(Long id) {
		sysUserMapper.deleteByPrimaryKey(id);
		sysUserRoleService.deleteBySysUser(id);
	}

	@Override
	public SysUser get(Long id) {
		return sysUserMapper.selectByPrimaryKey(id);
	}

	@Override
	public void update(SysUser user) {
		sysUserMapper.updateByPrimaryKey(user);
	}

}
